package com.comdo.newup_loan.module.setting;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.comdo.newup_loan.R;
import com.comdo.newup_loan.base.BaseActivity;
import com.comdo.newup_loan.model.NewUpVersionBean;
import com.comdo.newup_loan.util.TitleMenuUtil;
import com.comdo.newup_loan.util.update.ClientUpdate;

import butterknife.BindView;

/**
 * Created by liyuan on 2017/11/29.
 */

public class AboutActivity extends BaseActivity<AboutPresenter> implements AboutContract.View{
    @BindView(R.id.tv_name)
    TextView mNameText;
    @BindView(R.id.tv_version)
    TextView mVersionText;
    @BindView(R.id.tv_check)
    TextView mCheckText;
    private int currentVersionCode;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_about;
    }

    @Override
    protected void init() {
        super.init();
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        new TitleMenuUtil(AboutActivity.this,"关于",false);
        PackageManager manager = getPackageManager();
        try {
            PackageInfo info = manager.getPackageInfo(getPackageName(), 0);
            String appVersion = info.versionName; // 版本名
            // 版本号
            currentVersionCode = info.versionCode;
            mVersionText.setText("APP版本"+appVersion);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        mCheckText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.getVersion(0);
            }
        });
    }

    @Override
    public AboutPresenter getmPresenter() {
        return new AboutPresenter();
    }

    @Override
    public void presenterSetView() {
        if (mPresenter != null)
            mPresenter.setView(this);
    }

    @Override
    public void getVersion(NewUpVersionBean result) {
        if (currentVersionCode < result
                .getVersion()) {
            // 调取版本更新入口
            new ClientUpdate(AboutActivity.this,
                    result.getUrl(), result
                    .getContent(), result
                    .getVersionName()).check();

            Log.e("appVersion", "自己版本:"
                    + currentVersionCode);
            Log.e("appVersion",
                    "得到版本:" + result.getVersion());
            Log.e("appVersion",
                    "版本网址：" + result.getUrl());
        } else {
            showToast("未发现新版本");
        }
    }

    @Override
    public void Fail(String failMessage) {
        showToast(failMessage);
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
    }
}
